#!/usr/bin/python2.
#-*- coding: utf-8 -*-

import wmi
import sys
import re


wmisrv = None
cncfgs = None

fullIPPattern = re.compile(r"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$")
shortIPPattern = re.compile(r"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$")


class NetworkConfig:
	adapter = None
	ips = None
	masks = None
	gates = None
	dnses = None


def loadConfigFromXML(url):
	pass

def loadConfigFromKeyboard(params):
	pass

def config(cfg, device = 0):
	
	if not cfg or len(cfg) < 1:
		print "Error"
		exit()
	
	for objNicConfig in cncfgs:
		print objNicConfig.Index
		print objNicConfig.SettingID
		print objNicConfig.Description.encode("utf-8")
		for ip in objNicConfig.IPAddress: print str(ip)
		print str(objNicConfig.IPSubnet[0])
		print str(objNicConfig.DefaultIPGateway[0])
		print objNicConfig.DNSServerSearchOrder

	mode = 0
	result = None
	tagcfg = NetworkConfig()

	for i in range(0, len(cfg)):
		mode = mode + 2**i
	if len(cfg) > 1 and re.split("\.", cfg[0])[2] == re.split("\.", cfg[1])[2]:
		mode = 2 * mode - 1

	print mode
	n = 0
	if mode & 1:
		tagcfg.ips = [cfg[n]]
		tagcfg.masks = [str(cncfgs[device].IPSubnet[0])]
		n = n + 1

	if not(mode & 2) and len(cfg) > 1\
		and re.split("\.", cfg[0])[2] == re.split("\.", cfg[1])[2]:
		tagcfg.masks = [str(cncfgs[device].IPSubnet[0])]
	
	if mode & 2 and re.split("\.", cfg[0])[2] != re.split("\.", cfg[1])[2]:
		tagcfg.masks = [cfg[n]]
		n = n + 1

	if mode & 4:
		tagcfg.gates = [cfg[n]]
		n = n + 1

	if mode & 8:
		tagcfg.dnses = [cfg[n]]
		n = n + 1
	
	if mode & 16:
		tagcfg.dnses.append(cfg[n])
		n = n + 1
	
	print tagcfg.ips
	print tagcfg.masks
	print tagcfg.gates
	print tagcfg.dnses

	if tagcfg.ips and tagcfg.masks:
		result = cncfgs[device].\
			EnableStatic(IPAddress = tagcfg.ips,\
						 SubnetMask = tagcfg.masks)
	if tagcfg.gates:
		result = cncfgs[device].\
			SetGateways(DefaultIPGateway = tagcfg.gates,\
						GatewayCostMetric = [1])

	

def fixIP(ip, device = 0):
	
	global wmisrv
	global cncfgs

	if not wmisrv:
		wmisrv = wmi.WMI()
	if not cncfgs:
		cncfgs = wmisrv.Win32_NetworkAdapterConfiguration(IPEnabled = True)
	
	pattern = re.compile(r"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.")
	return pattern.match(str(cncfgs[device].IPAddress[0])).group() + ip


if __name__ == "__main__":

	args = ["NA"]
	if len(sys.argv) == 1:
		args = args + re.split(" ", raw_input("type ip:\n"))
	else:
		args = sys.argv

	cfg = [] 

	for i in range(1, len(args) < 6 and len(args) or 6):
		if fullIPPattern.match(args[i]):
			cfg.append(args[i])
		elif shortIPPattern.match(args[i]):
			cfg.append(i == 2 and "255.255." + args[i]\
						or fixIP(args[i]))
		else:
			print "error"
			exit()
	
	if len(args) > 2 and\
		re.split("\.", cfg[0])[2] == re.split("\.", cfg[1])[2]:
		cfg[1] = shortIPPattern.match(args[2]) and fixIP(args[2])\
					or args[2]

	print cfg
	wmisrv = wmi.WMI()
	cncfgs = wmisrv.Win32_NetworkAdapterConfiguration(IPEnabled = True)

	#cfg = loadConfig(None)
	config(cfg)	
